package cn.iocoder.yudao.module.trade.controller.admin.ordersafetychecktemplate;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.trade.controller.admin.ordersafetychecktemplate.vo.OrderSafetyCheckTemplatePageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.ordersafetychecktemplate.vo.OrderSafetyCheckTemplateRespVO;
import cn.iocoder.yudao.module.trade.controller.admin.ordersafetychecktemplate.vo.OrderSafetyCheckTemplateSaveReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.ordersafetychecktemplate.OrderSafetyCheckTemplateDO;
import cn.iocoder.yudao.module.trade.service.ordersafetychecktemplate.OrderSafetyCheckTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 安全检查服务")
@RestController
@RequestMapping("/trade/order-safety-check-template")
@Validated
public class OrderSafetyCheckTemplateController {

    @Resource
    private OrderSafetyCheckTemplateService orderSafetyCheckTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建安全检查服务")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check-template:create')")
    public CommonResult<Long> createOrderSafetyCheckTemplate(@Valid @RequestBody OrderSafetyCheckTemplateSaveReqVO createReqVO) {
        return success(orderSafetyCheckTemplateService.createOrderSafetyCheckTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新安全检查服务")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check-template:update')")
    public CommonResult<Boolean> updateOrderSafetyCheckTemplate(@Valid @RequestBody OrderSafetyCheckTemplateSaveReqVO updateReqVO) {
        orderSafetyCheckTemplateService.updateOrderSafetyCheckTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除安全检查服务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check-template:delete')")
    public CommonResult<Boolean> deleteOrderSafetyCheckTemplate(@RequestParam("id") Long id) {
        orderSafetyCheckTemplateService.deleteOrderSafetyCheckTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得安全检查服务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check-template:query')")
    public CommonResult<OrderSafetyCheckTemplateRespVO> getOrderSafetyCheckTemplate(@RequestParam("id") Long id) {
        OrderSafetyCheckTemplateDO orderSafetyCheckTemplate = orderSafetyCheckTemplateService.getOrderSafetyCheckTemplate(id);
        return success(BeanUtils.toBean(orderSafetyCheckTemplate, OrderSafetyCheckTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得安全检查服务分页")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check-template:query')")
    public CommonResult<PageResult<OrderSafetyCheckTemplateRespVO>> getOrderSafetyCheckTemplatePage(@Valid OrderSafetyCheckTemplatePageReqVO pageReqVO) {
        return success(orderSafetyCheckTemplateService.getOrderSafetyCheckTemplatePage(pageReqVO));
    }
    @GetMapping("/select/list")
    @Operation(summary = "获得安全检查服务列表下拉框")
    public CommonResult<List<OrderSafetyCheckTemplateRespVO>> getOrderSafetyCheckTemplateSelectList(@Valid OrderSafetyCheckTemplatePageReqVO pageReqVO) {
        List<OrderSafetyCheckTemplateRespVO> list = orderSafetyCheckTemplateService.getOrderSafetyCheckTemplateSelectList(pageReqVO);
        return success(list);
    }

    @GetMapping("/list")
    @Operation(summary = "获得燃气预警设备安装列表")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check-template:query')")
    public CommonResult<List<OrderSafetyCheckTemplateRespVO>> getOrderSafetyCheckTemplateList(@Valid OrderSafetyCheckTemplatePageReqVO pageReqVO) {
        return success(orderSafetyCheckTemplateService.getOrderSafetyCheckTemplateList(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出安全检查服务 Excel")
    @PreAuthorize("@ss.hasPermission('trade:order-safety-check-template:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderSafetyCheckTemplateExcel(@Valid OrderSafetyCheckTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderSafetyCheckTemplateRespVO> list = orderSafetyCheckTemplateService.getOrderSafetyCheckTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "安全检查服务.xls", "数据", OrderSafetyCheckTemplateRespVO.class,
                        list);
    }

}